import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:uuid/uuid.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../pages/utils/bt_constants.dart';
import '../pages/utils/bt_toast_utils.dart';

stringToMd5(String str) {
  return md5.convert(utf8.encode(str)).toString();
}

String listToString(List<String> list) {
  String listString = "";
  for (var element in list) {
    if (element.isNotEmpty) {
      listString = "$listString$element,";
    }
  }
  if (listString.isNotEmpty) {
    listString = listString.substring(0, listString.length - 1);
  }

  return listString;
}

Future<String> getDeviceId() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var deviceId = prefs.getString('device_id') ?? '';
  if (deviceId.isEmpty) {
    var uuid = const Uuid();
    await prefs.setString('device_id', uuid.v4());
  }

  return deviceId;
}

Future<bool> isGuideShowed() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool showed = prefs.getBool(guideShowedFlag) ?? false;

  return showed;
}

Future<bool> isFirstOpened() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool opened = prefs.getBool(firstOpenedFlag) ?? true;

  return opened;
}

Future<void> setFirstOpened(bool opened) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setBool(firstOpenedFlag, opened);
}

Future<void> setGuideShowed(bool showed) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setBool(guideShowedFlag, showed);
}

Future<void> resetAppConfigVersion() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setString(appConfigVersion, '0');
}

String getCurrentDate() {
  DateTime now = DateTime.now();
  String formattedDate = DateFormat('d\'th\' MMMM').format(now);

  return formattedDate;
}

Future<void> copyTextToClipboard(String text) async {
  await Clipboard.setData(ClipboardData(text: text));
  showToast('Successfully copied');
}

String formatTimestampToMonthDayHourMinute(int timestamp) {
  // 将毫秒时间戳转换为DateTime对象
  DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);

  // 格式化日期为月份-日期 小时:分钟
  String formattedDate =
      '${dateTime.month}-${dateTime.day} ${_addLeadingZero(dateTime.hour)}:${_addLeadingZero(dateTime.minute)}';

  return formattedDate;
}

String formatTimestampToYearMonthDayHourMinute(int timestamp) {
  // 将毫秒时间戳转换为DateTime对象
  DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);

  // 格式化日期为月份-日期 小时:分钟
  String formattedDate =
      '${dateTime.year}-${dateTime.month}-${dateTime.day} ${_addLeadingZero(dateTime.hour)}:${_addLeadingZero(dateTime.minute)}';

  return formattedDate;
}

// 在小时和分钟前添加前导零
String _addLeadingZero(int number) {
  return number.toString().padLeft(2, '0');
}

bool compareDateOfBirth(String dateOfBirth, int comparedAge) {
  DateTime birthday = DateTime.parse(dateOfBirth);

  // 计算年龄
  DateTime now = DateTime.now();
  int age = now.year - birthday.year;
  if (now.month < birthday.month || (now.month == birthday.month && now.day < birthday.day)) {
    age--;
  }

  return age < comparedAge;
}
